﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ChineseChess.Game.Rules
{
    class BingRule:Rule
    {
        private List<Piece> pies = null;
        public override bool IsDown { get; set; }
        public override List<Point> getNextPos(Piece pie, List<Piece> pies)
        {
            this.pies = pies;
            List<Point> pList = new List<Point>();
            int x = pie.X + 1;
            int y = pie.Y;
            this.Add(pList, pie, x, y);

            x = pie.X - 1;
            y = pie.Y ;
            this.Add(pList, pie, x, y);

            x = pie.X ;
            y = pie.Y  + 1;
            this.Add(pList, pie, x, y);

            x = pie.X ;
            y = pie.Y  - 1;
            this.Add(pList, pie, x, y);
            return pList;
        }

        private void Add(List<Point> pList, Piece pie, int x, int y)
        {
            if (IsRight(pie, x, y))
            {
                pList.Add(new Point(x, y));
            }
        }

        private bool IsRight(Piece pie, int x, int y)
        {
            if (x >= 9 || x < 0 || y < 0 || y >= 10)
            {
                return false;
            }

            if (IsDown && pie.Y < y)
            {   //红子只能向上
                return false;
            }
            else if (!IsDown && pie.Y > y)
            {
                return false;
            }
            int riverIndex = IsDown ? 5 : 4; // 河界
            if (IsDown && y >= riverIndex && pie.X != x)
            {   //未过河不可以左右走
                return false;
            }
            else if(!IsDown && y <= riverIndex && pie.X != x)
            {
                return false;
            }

            Piece p = Chessboard.getPieByXy(x, y,pies);
            if (p != null && p.isRed == pie.isRed)//判断要走的坐标是否有自己一方的棋
            {
                return false;
            }
            return true;
        }
    }
}
